package com.fanbai.score.user.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.fanbai.score.R;
import com.fanbai.score.common.Manager.ActivityManager;
import com.fanbai.score.common.Utils.ImageUtils;
import com.fanbai.score.common.Utils.ToastUtils;
import com.fanbai.score.common.base.BaseActivity;
import com.fanbai.score.common.http.CallBack;
import com.fanbai.score.user.bean.RedInfo_Bean;
import com.fanbai.score.user.http.UserScorea_RequestNet_Utils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by acer on 2017/5/13.
 * 兑换红包
 */

public class RedExchange_Activity extends BaseActivity{

    private int index = 0;

    @BindView(R.id.view_recyclerView)
    RecyclerView mRecyclerView;

    @OnClick(R.id.userActivityRedExchange_duiHuanBut)
    public void onClick(){
        if(dataList != null && dataList.size() > index){
            exchargeredbag(dataList.get(index).getRbid());
        }
    }

    @Override
    protected int getLayoutID() {
        return R.layout.user_activity_redexchange;
    }

    @Override
    public String getTitleName() {
        return "兑换红包";
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        getRedbagInfos();
    }

    @Override
    protected void initListener() {
    }
    List<RedInfo_Bean> dataList;
    private void setData(final List<RedInfo_Bean> dataList){
        this.dataList = dataList;
       /* List<String> dataList = new ArrayList<>();
        for (int i = 0; i < 10 ; i++){
            dataList.add("item"+i);
        }*/
        mRecyclerView.setLayoutManager(new LinearLayoutManager(ctx));
        CommonAdapter adapter = new CommonAdapter<RedInfo_Bean>(ctx, R.layout.user_item_redbag,
                dataList){
            @Override
            protected void convert(ViewHolder holder, RedInfo_Bean s, int position) {
                holder.setText(R.id.userItemRedbag_redbagName, s.getRbjine());
                holder.setText(R.id.userItemRedbag_redbagHine, s.getRbdesc());
                ImageUtils.show(ctx, s.getRbimage(), (ImageView) holder.getView(R.id.userItemRedbag_redbagImg), R.mipmap.icon_img_defeated);
            }
        };
        mRecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                index = position;
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    @Override
    public int getTitleLeftName() {
        return R.mipmap.back_click;
    }

    @Override
    public void onClickTitleLeft() {
        ActivityManager.getInstance().finishActivity();
    }

    private void getRedbagInfos(){
        showDialog();
        UserScorea_RequestNet_Utils.getRedbagInfos(ctx, new CallBack<List<RedInfo_Bean>>() {
            @Override
            public void onSuccess(String code, String msg, List<RedInfo_Bean> beans) {
                hideDialog();
                setData(beans);
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                hideDialog();
                ToastUtils.ErrorImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                hideDialog();
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }

    private void exchargeredbag(String rbid){
        showDialog();
        UserScorea_RequestNet_Utils.exchargeRedbag(ctx, rbid, new CallBack<String>(){
            @Override
            public void onSuccess(String code, String msg, String beans) {
                hideDialog();
                ToastUtils.RightImageToast(ctx, msg);
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                hideDialog();
                ToastUtils.ErrorImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                hideDialog();
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }
}
